package com.heatup.apt.management.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.management.service.accessdevice.AccessDeviceService;
import com.heatup.apt.management.service.store.StoreService;
import com.heatup.apt.model.AccessDevice;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by blank on 2017/2/17.
 */
@Controller
@RequestMapping("/system/accessDevice")
public class AccessDeviceController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(AccessDevice.class);

    @Autowired
    private AccessDeviceService accessDeviceService;

    @Autowired
    private StoreService storeService;


    /**
     * 添加/编辑 门禁设备
     *
     * @param id 门禁 ID
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView addUI(Long id) {
        ModelAndView modelAndView = new ModelAndView("accessDevice/access-device-add");
        if (id != null) {
            modelAndView.addObject("access", accessDeviceService.selectById(id));
        }
        modelAndView.addObject("stores", storeService.queryAllStore());
        return modelAndView;
    }

    /**
     * 门禁设备列表页
     *
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView listUI() {
        //test
        ModelAndView modelAndView = new ModelAndView("accessDevice/access-device-list");
        modelAndView.addObject("stores", storeService.queryAllStore());
        return modelAndView;
    }

    /**
     * 门禁设备分页
     *
     * @param accessDevice 查询参数
     * @return 分页数据
     */
    @RequestMapping(value = "/accessDeviceList")
    @ResponseBody
    public String accessDeviceList(@RequestParam int pageIndex,
                                   @RequestParam int pageSize,
                                   AccessDevice accessDevice) {

        Page<AccessDevice> page = new Page<>(pageIndex, pageSize);
        page = accessDeviceService.queryAccessDeviceListPage(page, accessDevice);
        return JsonUtils.toJsonStrWithDateFormat(page);
    }

    /**
     * 保存门禁设备
     *
     * @param accessDevice 门禁设备
     * @return success 成功 / false 失败
     */
    @RequestMapping(value = "/accessDeviceSave")
    @ResponseBody
    public String accessDeviceSave(AccessDevice accessDevice) {

        Map<String, Object> resultMap = new HashMap<>();

        if (accessDevice == null) {
            resultMap.put("result", false);
            resultMap.put("msg", "没有数据需要保存");

        } else {

            try {

                resultMap.put("result", accessDeviceService.saveAccessDevice(accessDevice));

            } catch (Exception e) {
                e.printStackTrace();
                log.error("保存门禁设备出错 " + e.getMessage());
                resultMap.put("msg", "保存门禁设备出错 " + e.getMessage());
            }

        }

        return JsonUtils.toJsonStr(resultMap);
    }

    /**
     * 查询当前门禁设备操房 ID 是否重复
     *
     * @return false 错误(有重复ID)/ success 正常(无重复 ID)
     */
    @RequestMapping(value = "/checkWorkoutNum")
    @ResponseBody
    public String checkWorkoutNum(AccessDevice accessDevice) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = false;
        if (accessDevice == null || accessDevice.getWorkoutRoomNum() == null) {
            resultMap.put("msg", "操房ID不能为空");
            result = false;
        } else {
            result = accessDeviceService.selectCount(accessDevice)== 0;
            if (result) {
                resultMap.put("msg", " 操房ID无重复");
            } else {
                resultMap.put("msg", "操房ID重复");
            }
        }
        resultMap.put("result", result);
        return JsonUtils.toJsonStr(resultMap);
    }

    /**
     * 查询当前门禁设备是否有重复的设备 ID
     *
     * @return false 错误(有重复ID)/ success 正常(无重复 ID)
     */
    @RequestMapping(value = "/checkSerialId")
    @ResponseBody
    public String checkSerialId(AccessDevice accessDevice) {
        Map<String, Object> resultMap = new HashMap<>();
        boolean result = false;
        if (accessDevice == null || StringUtils.isBlank(accessDevice.getDeviceSerialId())) {
            resultMap.put("msg", "设备序列号不能为空");
            result = false;
        } else {
            result =accessDeviceService.selectCount(accessDevice) == 0;
            if (result) {
                resultMap.put("msg", "无重复设备序列号");
            } else {
                resultMap.put("msg", "有重复设备序列号");
            }
        }
        resultMap.put("result", result);
        return JsonUtils.toJsonStr(resultMap);
    }

}
